Page({
  data: {
    userInfo: wx.getStorageSync('userInfo') || {
      name: 'user',
      gender: '男',
      age: 21,
      phone: '18859309207'
    },
    medicalHistory: wx.getStorageSync('medicalHistory') || [],
    examinationReports: wx.getStorageSync('examinationReports') || [],
    medicationRecords: wx.getStorageSync('medicationRecords') || [],
    allergyHistory: wx.getStorageSync('allergyHistory') || [],
    searchKeyword: ''
  },

  onLoad() {
    this.loadLocalData(); // 加载本地数据（含模拟数据）
  },

  // 加载本地数据，若无则生成模拟数据
  loadLocalData() {
    // ========== 检查报告：模拟4条数据 ==========
    let examinationReports = wx.getStorageSync('examinationReports') || [];
    if (examinationReports.length === 0) {
      examinationReports = [
        { id: 1, date: '2025-06-01', type: '血常规', result: '正常' },
        { id: 2, date: '2025-05-20', type: '胸部CT', result: '未见异常' },
        { id: 3, date: '2025-05-10', type: '尿常规', result: '正常' },
        { id: 4, date: '2025-04-25', type: '心电图', result: '窦性心律' }
      ];
      wx.setStorageSync('examinationReports', examinationReports);
    }

    // ========== 用药记录：模拟3条数据 ==========
    let medicationRecords = wx.getStorageSync('medicationRecords') || [];
    if (medicationRecords.length === 0) {
      medicationRecords = [
        { id: 1, startDate: '2025-05-01', endDate: '2025-05-07', drugName: '布洛芬', dosage: '1片/日' },
        { id: 2, startDate: '2025-04-15', endDate: '2025-04-20', drugName: '阿莫西林', dosage: '2粒/日' },
        { id: 3, startDate: '2025-03-10', endDate: '2025-03-15', drugName: '维生素C', dosage: '1片/日' }
      ];
      wx.setStorageSync('medicationRecords', medicationRecords);
    }

    // 更新页面数据
    this.setData({
      userInfo: wx.getStorageSync('userInfo') || {
        name: 'user',
        gender: '男',
        age: 21,
        phone: '18859309207'
      },
      medicalHistory: wx.getStorageSync('medicalHistory') || [],
      examinationReports,
      medicationRecords,
      allergyHistory: wx.getStorageSync('allergyHistory') || []
    });
  },

  // 搜索输入处理
  onSearchInput(e) {
    this.setData({ searchKeyword: e.detail.value });
  },

  // 上传信息（示例：图片上传）
  uploadInfo(type) {
    wx.chooseImage({
      count: 1,
      success: (res) => {
        const newRecord = { id: Date.now(), path: res.tempFilePaths[0], date: new Date().toLocaleDateString() };
        this.setData({ [type]: [...this.data[type], newRecord] }, () => {
          wx.setStorageSync(type, this.data[type]);
        });
      }
    });
  },

  // 编辑信息（跳转编辑页）
  editInfo(e) {
    const { type, index } = e.currentTarget.dataset;
    wx.navigateTo({ url: `/pages/health-record/edit?type=${type}&index=${index}` });
  },

  // 生命周期函数（保持默认，可扩展）
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {}
});